<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AnswerSurveyOption;
use App\Models\AnswerSurvey;
use App\Models\AnswerSurveySubject;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AnswerSurveyOptionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(AnswerSurveyOption::with(['answer', 'subject']), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('answer.title', admin_trans('answer-survey-option.fields.answer_title'));
            $grid->column('subject.title', admin_trans('answer-survey-option.fields.subject_title'));
            $grid->column('serial');
            $grid->column('content');
            $grid->column('state')->using(admin_trans('answer-survey-option.options.state'));
            $grid->column('status')->using(admin_trans('answer-survey-option.options.status'));
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, AnswerSurveyOption::with(['answer', 'subject']), function (Show $show) {
            $show->field('id');
            $show->field('answer.title', admin_trans('answer-survey-option.fields.answer_title'));
            $show->field('subject.title', admin_trans('answer-survey-option.fields.subject_title'));
            $show->field('serial');
            $show->field('content');
            $show->field('state')->using(admin_trans('answer-survey-option.options.state'));
            $show->field('status')->using(admin_trans('answer-survey-option.options.status'));
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AnswerSurveyOption(), function (Form $form) {
            $form->display('id');
            $form->select('answer_id')
                ->options(AnswerSurvey::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id'))
                ->required()
                ->load('subject_id', 'answer-has-subject');
            $form->select('subject_id')
                ->options(AnswerSurveySubject::where('status', DEFAULT_STATUS_SUCCESS)->pluck('title', 'id'))
                ->required();
            $form->text('serial');
            $form->text('content');
            $form->radio('state')->options(admin_trans('answer-survey-option.options.state'))->default('0');
            $form->radio('status')->options(admin_trans('answer-survey-option.options.status'))->default('1');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function hasSubject(Request $request)
    {
        $answer_id = $request->get('q');

        return AnswerSurveySubject::where('answer_id', $answer_id)->get(['id', DB::raw('title as text')]);
    }
}
